/*
 * OverviewAdapter.java
 *
 * Copyright(c) 2011
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.gerengdic.overview;

import de.studiorutton.gerengdic.R;
import de.studiorutton.gerengdic.overview.Overview.ItemBundle;
import de.studiorutton.gerengdic.overview.Overview.ItemType;
import de.studiorutton.texttovoice.PlayerWorker;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class OverviewAdapter extends BaseAdapter {
	Overview o;
	IOverviewListener ol;
	
	public OverviewAdapter( Overview o ) {
		this.o = o;
	}
	
	public void setOverviewListener( IOverviewListener ol ) {
		this.ol = ol;
	}

	public int getCount() {
		return o.size();
	}

	public Object getItem(int position) {
		return o.getItem( position );
	}

	public long getItemId(int arg0) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		RelativeLayout rl;
		if (convertView == null) {
			rl = (RelativeLayout) LayoutInflater.from(ol.getContext()).inflate(
					R.layout.overviewitem, parent, false);
		} else {
			rl = (RelativeLayout) convertView;
		}
		
		ItemBundle item = o.getItem( position );
		
		String leftString = item.stringLeft;
		if ( leftString != null ) {
			TextView leftText = (TextView) rl.findViewById( R.id.itemleft );
			leftText.setText( leftString );
			Button leftButton = (Button)rl.findViewById( R.id.buttonleft );
			leftButton.setBackgroundResource( R.drawable.audio_out_32_23);
			leftButton.setOnClickListener( new ButtonOnClickListener( PlayerWorker.LANG_DE, leftString ));
			if ( item.subType == ItemType.LATER_ITEM ) {
				TextView leftDot = (TextView) rl.findViewById( R.id.dotleft );
				leftDot.setText( "●" );
			}
		}
		
		String rightString = item.stringRight;
		
		if ( rightString != null ) {
			TextView rightText = (TextView) rl.findViewById( R.id.itemright );
			rightText.setText( rightString );
			Button rightButton = (Button)rl.findViewById( R.id.buttonright );
			rightButton.setBackgroundResource( R.drawable.audio_out_32_23 );
			rightButton.setOnClickListener( new ButtonOnClickListener( PlayerWorker.LANG_EN, rightString ));
			if ( item.subType == ItemType.LATER_ITEM ) {
				TextView rightDot = (TextView) rl.findViewById( R.id.dotright );
				rightDot.setText( "●" );
			}
		}
		
		return rl;
	}
	
	class ButtonOnClickListener implements OnClickListener {
		final String text;
		final int language;
		
		public ButtonOnClickListener( int language, String text ) {
			this.language = language;
			this.text = text;
		}

		public void onClick(View v) {
			Button b = (Button) v;
			b.setBackgroundResource( R.drawable.audio_out_inverse_32_23);
			o.play( language, text);
		}
		
	}

}
